package com.lin.sms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lin.sms.dao.IScoreDAO;
import com.lin.sms.dao.IScoreDAOByPage;
import com.lin.sms.domain.Score;
import com.lin.sms.domain.User;
import com.lin.sms.mapper.ScoreMapper;
import com.lin.sms.page.PageResult;


@Service
public class ScoreService {
    
    @Autowired
    ScoreMapper scoreMapper;
    
    @Autowired
    IScoreDAO scoreDao;
    @Autowired
    IScoreDAOByPage scoreDaoByPage;
    
    public PageResult queryPage(User user, Integer currentPage, Integer pageSize) {
        List<Score> listData = null;
        Integer totalCount;
        //根据当前页和页面大小计算出当前行
        Integer currentRow = (currentPage-1)*pageSize;
        if (user.getClassId().equals("*")||user.getClassId().equals("1006")) {
         listData = scoreMapper.listScore(currentRow, pageSize);
         totalCount = scoreMapper.count();
        } else {
            listData = scoreMapper.listScoreByClassId(user.getClassId(), currentPage, pageSize);
            totalCount = scoreMapper.countByClassId(user.getClassId());
        }
    
        PageResult pageResult = new PageResult(listData,totalCount,currentPage,pageSize);
        return pageResult;
     }
    /**
     * 查询所有成绩
     */
    public List<Score> queryScores(User user) {
        System.out.println("do queryScores");
        List<Score> list = scoreDao.list(user.getClassId());
        return list;
    }
    
    /**
     * 分页的方式查询所有成绩,并封装到PageResult中
     */
    public PageResult queryScoresResult(User user, Integer currentPage, Integer pageSize) {
       PageResult pageResult = scoreDaoByPage.listPageResult(user.getClassId(), currentPage, pageSize);
        return pageResult;
    }
    
    /**
     * 查询最好的成绩
     */
    public List<Score> queryBestScore(User user) {
        List<Score> list = scoreDao.getBestScore(user.getClassId());
        return list;
    }
    
    /**
     * 查询中文最好的成绩
     */
    public List<Score> queryChinesBest(User user) {
        List<Score> list = scoreDao.getChineseBestScore(user.getClassId());
        return list;
    }
    
    /**
     * 查询数学最差的成绩
     */
    public List<Score> qureyMathWorst(User user) {
        List<Score> list = scoreDao.getMathWorstScore(user.getClassId());
       return list;
    }
    
    /**
     * 统计英语大于90分的人数
     */
    public int qureyEnglishMoreThan90 (User user) {
        int value = scoreDao.getEnglishMoreThan90(user.getClassId());
       return value;
    }
    
    /**
     * 计算英语平均分
     */
    public double qureyEnglishAverage(User user) {
        double  value = scoreDao.getEnglishAverageScore(user.getClassId());
        return value;
    }
    
    /**
     * 查询某个人的成绩
     */
    public List<Score> queryOne(User user,String studentName) {
        System.out.println(user);
            List<Score> scores = scoreDao.get(studentName, user.getClassId());
            System.out.println(scores);
            return scores;
    }
}
